//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../common/js/ajax.js');
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
var page = 1;
var switch_p = true
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		priceTk: false,
		store_id: 0,
		cur_Index: 0,
		cur_stock: 0,
		cur_Id: 0,
		list: [],
		tips: false,
		isIpx:app.globalData.isIpx?true:false
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this;
		this.load = this.selectComponent("#loading");
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		_this.setData({
			store_id: e.id
		})
		Axios.get(app.globalData.baseUrl + 'App/Store/Goods/store_goods', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			type: 0,
			p: 1
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					list: data
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '待售商品'
		})
	},
	preventTouchMove: function() {

	},
	bubbling: function() {

	},
	onReachBottom: function() {
		var _this = this;
		wx.showLoading({
			title: '玩命加载中',
		})
		if (switch_p) {
			switch_p = false
			page = page + 1;
			Axios.get(app.globalData.baseUrl + 'App/Store/Goods/store_goods?p=' + page, {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
				store_id: this.data.store_id,
				type: 0
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = _this.data.list.concat(data)
					_this.setData({
						list: temp
					})
					switch_p = true
					wx.hideLoading();
				} else {
					_this.showModal(res)
				}
			})
		}
	},
	handleCloseTap: function() {
		this.setData({
			priceTk: false,
			tips: false
		})
	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	handleDelTap: function(e) { //一键删除
		var _this = this;
		var index = e.currentTarget.dataset.index
		var id = e.currentTarget.dataset.id
		var list = this.data.list;
		var value = this.data.list[index].value;
		var len = value.length;
		Axios.post(app.globalData.baseUrl + 'App/Store/Goods/store_del_goods', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			goods_id: id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				if (len > 1) {
					value.forEach(function(item, key) {
						if (item.id == id) {
							value.splice(key, 1)
							_this.setData({
								['list[' + index + '].value']: value
							})
						}
					})
				} else {
					list.splice(index, 1)
					_this.setData({
						list: list
					})
				}
			} else {
				_this.showModal(res)
			}
		})
	},
	handleMonckTap: function(e) { //修改库存弹窗
		var _this = this;
		var index = e.currentTarget.dataset.index;
		var id = e.currentTarget.dataset.id;
		var stock = e.currentTarget.dataset.stock;
		this.setData({
			priceTk: true,
			cur_Index: index,
			cur_stock: stock,
			cur_Id: id,
		})
	},
	bindPriceTap: function(e) {
		console.log(e.detail.value)
		this.setData({
			cur_stock: e.detail.value
		})
	},
	handleSureTap: function(e) { //修改库存
		var stock = this.data.cur_stock
		if (stock == '') {
			wx.showModal({
				content: '请填写修改的数量',
				showCancel: false,
			})
			return false
		} else if (stock == 0) {
			wx.showModal({
				content: '数量不能为空',
				showCancel: false,
			})
			return false
		}
		var _this = this;
		var index = e.currentTarget.dataset.index;
		var id = e.currentTarget.dataset.id;
		var list = this.data.list[index].value;
		Axios.post(app.globalData.baseUrl + 'App/Store/Goods/change_stock', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			goods_id: id,
			stock: stock
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				list.forEach(function(item, key) {
					if (item.id == id) {
						_this.setData({
							['list[' + index + '].value.[' + key + '].stock']: stock
						})
					}
				})
				_this.setData({
					tips: true
				})
			} else {
				_this.showModal(res)
			}
		})
	},
	handleSignTap: function(e) { //标记已售
		var _this = this;
		var index = e.currentTarget.dataset.index;
		var id = e.currentTarget.dataset.id;
		var list = this.data.list[index].value;
		Axios.post(app.globalData.baseUrl + 'App/Store/Goods/set_sale', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			goods_id: id,
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.showModal(res)
				list.forEach(function(item, key) {
					if (item.id == id) {
						_this.setData({
							['list[' + index + '].value.[' + key + '].stock']: 0
						})
					}
				})
			} else {
				_this.showModal(res)
			}
		})
	},
	handleAgainTap: function(e) { //再次发布
		var _this = this;
		var index = e.currentTarget.dataset.index;
		var id = e.currentTarget.dataset.id;
		var list = this.data.list[index].value;
		Axios.post(app.globalData.baseUrl + 'App/Store/Goods/sale_again', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			goods_id: id,
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				wx.showModal({
					content: '发布成功',
					showCancel: false,
					success: function() {
						Axios.get(app.globalData.baseUrl + 'App/Store/Goods/store_goods', {
							uid: _this.uid,
							time: _this.timesp,
							sign: _this.Md5,
							store_id: _this.data.store_id,
							type: 0,
							p: 1
						}, (res) => {
							console.log(res)
							if (res.status == 1) {
								var data = res.info
								_this.setData({
									list: data
								})
							} else {
								_this.showModal(res)
							}
						})
					}
				})
			} else {
				_this.showModal(res)
			}
		})
	},

})
